﻿Module InitializeFileLocations
    Private Debugged As Boolean
    Private VersionInfo As String
    Private Function MissingFileShow(ByVal File As String, Optional ByVal MissingFlash As Boolean = False)
        If MissingFlash = False Then
            MainProgram.updatebars()
            MainProgram.SND("/RERROR: " + File + " Database Corrupt!")
            MainProgram.SND("    /yTry upgrading your resources to the latest version.")
            MainProgram.SND("    /yYou can continue playing at your own /Rrisk/y.")
            MainProgram.SND("    /yResuming...")
        Else
            MessageBox.Show("MainFlash.swf is missing from the resources folder", "Missing MainFlash.swf", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Application.Exit()
        End If
        Return 0
    End Function
    Private Function CheckFile(ByVal fileloc As String, ByVal fileshort As String) As String
        Dim TheResult As String
        If My.Computer.FileSystem.FileExists(MainProgram.ResourcesDir + fileloc) = False Then
            MissingFileShow(fileshort)
            TheResult = ""
        Else
            TheResult = Replace(MainProgram.ResourcesDir + fileloc, "\\", "\")
        End If
        Return TheResult
    End Function
    Private Function CheckDirectory(ByVal dirloc As String, ByVal fileshort As String) As String
        Dim TheResult As String
        If My.Computer.FileSystem.DirectoryExists(MainProgram.ResourcesDir + dirloc) = False Then
            MissingFileShow(fileshort)
            TheResult = ""
        Else
            TheResult = Replace(MainProgram.ResourcesDir + dirloc, "\\", "\")
        End If
        Return TheResult
    End Function
    Public Sub Begin()
        'this attempts to locate the resources folder and if it isn't found in it's default spot
        'it allows the user to find the folder and saves the settings for future use. This allows
        'a programmer who is constantly debugging things not to have two different resource folders.
        If My.Computer.FileSystem.DirectoryExists(CurDir() + "\Resources") Then
            MainProgram.ResourcesDir = CurDir() + "\Resources\"
        Else
            Debugged = True
            If My.Computer.FileSystem.FileExists(CurDir() + "\ResourceLocation.TG") Then 'this saves the location of the database for future reference if not found in correct spot
                MainProgram.ResourcesDir = MainProgram.GetFileContents(CurDir() + "\ResourceLocation.TG")
                If My.Computer.FileSystem.DirectoryExists(MainProgram.ResourcesDir) = False Then 'if the file is incorrect, or the resources has moved then
                    MessageBox.Show("The resources directory has moved, please re-locate the Version.TG file.", "Error: 332 - Directory Moved", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Dim FindResourcesFolder As New OpenFileDialog
                    FindResourcesFolder.InitialDirectory = CurDir()
                    FindResourcesFolder.Multiselect = False
                    FindResourcesFolder.Filter = "Terminal Groupings (*.TG)|*.TG|Text Files (*.txt)|*.txt"
                    FindResourcesFolder.Title = "Please Locate PLOS Resources File : Version.TG"
                    FindResourcesFolder.ShowHelp = False
                    FindResourcesFolder.CheckFileExists = True
                    FindResourcesFolder.ShowDialog()
                    MainProgram.ResourcesDir = Replace(FindResourcesFolder.FileName, "Version.TG", "")
                    MainProgram.SaveTextToFile(MainProgram.ResourcesDir, CurDir() + "\ResourceLocation.TG") 'save the new location
                End If
            Else
                Dim FindResourcesFolder As New OpenFileDialog
                FindResourcesFolder.InitialDirectory = CurDir()
                FindResourcesFolder.Multiselect = False
                FindResourcesFolder.Filter = "Terminal Groupings (*.TG)|*.TG|Text Files (*.txt)|*.txt"
                FindResourcesFolder.Title = "Please Locate PLOS Resources File : Version.TG"
                FindResourcesFolder.ShowHelp = False
                FindResourcesFolder.CheckFileExists = True
                FindResourcesFolder.ShowDialog()
                MainProgram.ResourcesDir = Replace(FindResourcesFolder.FileName, "Version.TG", "")
                MainProgram.SaveTextToFile(MainProgram.ResourcesDir, CurDir() + "\ResourceLocation.TG", MainProgram.sErr, True)
            End If
        End If
        'checks to see if there resource directory was found, or whether there were errors in which case... exit application, we
        'don't want to have any more errrors
        If MainProgram.ResourcesDir = "" Then
            MessageBox.Show("Closing application to avoid errors, resources folder not found", "Error 801 : Resources Missing", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        End If
        'Attempts to find the main flash. if this isn't found... there isn't a reason to run the application, as it's the 
        'container for all information. The function closes the application, optional "true"
        MainProgram.MainFlash.Movie = CheckFile("\Flash\MainFlash.swf", "MainFlash.swf")
        'Attempts to find the environment libraries
        MainProgram.DirEnvironments = CheckFile("\Databases\World\Environments.TG", "Environment")
        'Attempts to find music files
        MainProgram.DirMusic = CheckDirectory("\Databases\Music\", "Music Files")
        Dim test As String
        'Attempts to find the class libraries
        MainProgram.DirClasses = CheckFile("\Databases\Classes\Page.TG", "Page Class Library")
        test = MainProgram.DirClasses
        'Attempts to find the mobile database
        MainProgram.DirMobs = CheckFile("\Databases\Mobiles\Mobs.TG", "Mobile")
        'Attempts to find the help database
        MainProgram.DirHelp = CheckFile("\Databases\Help\Commands.TG", "Help")
        'Attempts to find the abilities database
        MainProgram.DirAbilities = CheckFile("\Databases\Abilities\Abilities.TG", "Abilitites")
        'Attempts to find the player save file directory
        MainProgram.DirPlayer = CheckDirectory("\Saves\", "Player Save")
        test = MainProgram.DirPlayer
        'Attempts to find the starting map base
        MainProgram.DirMaps = CheckFile("\Databases\World\RuinsOfVharmasBase.TG", "MapBase (Ruins of Vharmas)")
        'Attempts to find the starting map header
        MainProgram.DirMapCur = CheckFile("\Databases\World\RuinsOfVharmas.TG", "Map (Ruins of Vharmas)")
        'Attempts to find Map Header
        MainProgram.DirMapHeader = CheckFile("\Databases\World\MapHeader.TG", "MapHeader")
        'Attempts to find Items
        MainProgram.DirItems = CheckFile("\Databases\Items\Items.TG", "Item")
        'Attempts to find Emotes
        MainProgram.DirEmotes = CheckFile("\Databases\Emotes\Emotes.TG", "Emote")
        'Attempts to find Scripts
        MainProgram.DirScripts = CheckFile("\Databases\Mobiles\Scripts.TG", "Script")
        'Attempts to find Version Information
        If My.Computer.FileSystem.FileExists(MainProgram.ResourcesDir + "\Version.TG") = False Then
            MissingFileShow("Version Information")
        Else
            VersionInfo = MainProgram.GetFileContents(MainProgram.ResourcesDir + "\Version.TG", MainProgram.sErr)
        End If
        'Shows the version information
        If VersionInfo <> "" Then
            MainProgram.Text = "Plains of Sedia v" + Diagnostics.FileVersionInfo.GetVersionInfo(Application.ExecutablePath).FileVersion + " [Resource State: " + Mid(VersionInfo, InStr(VersionInfo, "STATE#&(") + 8, InStr(VersionInfo, ")") - InStr(VersionInfo, "STATE#&(") - 8) + "]"
            MainProgram.Text += " [Resource Version: " + Mid(VersionInfo, InStr(VersionInfo, "VERSION#&(") + 10, InStrRev(VersionInfo, ")") - InStr(VersionInfo, "VERSION#&(") - 10) + "]"
        End If
    End Sub
End Module
